<?php
/**
 * 后台管理公共类
 *
 * @package     Include  (controller)
 * @author      hary <hary@taomee.com>
 * @version     $ID 2010-3-31 $
 */
class Admin_common extends Controller
{

	private $result;

	const   TRY_LOGIN_NUM = 2; //最多尝试次数为TRY_LOGIN_NUM+1

	/**
	 * 构造函数
	 */
	function __construct()
	{
		parent::__construct();
		$this->loadModel('user_model');

		$this->tpl->template_dir = SYS_PATH  . 'template/admin/common/';
	}

	/**
	 * 默认执行方法
	 *
	 * @return response
	 */
	function index()
	{
		if($this->isLogin())
		{
			$this->tpl->template_dir = SYS_PATH  . 'template/admin/';
			$this->tpl->assign("user",$_SESSION['SURVEY']['USER']);
			$this->tpl->display("index.html");
		}
		else
		{
			$this->tpl->display("login.html");
		}
	}

	/**
	 * 用户登录
	 * @return response
	 */
	function login()
	{
		$tpl_name = "login.html";
		$method   = $this->in_var['request_method'];

		if($this->isLogin())
		{
			$this->tpl->template_dir = SYS_PATH  . 'template/admin/';

			$this->tpl->display('index.html');

			die;
		}
		
		if($method == "post")
		{
			$name 	   = trim($this->in_var['name']);
			$pwd 	   = $this->in_var['pass'];
			$seccode   = trim(strtolower($this->in_var['seccode']));

			if($_SESSION['login_error_num'] > self::TRY_LOGIN_NUM)
			{
				$code = trim(strtolower($_SESSION['login_check_number']));
					
				unset($_SESSION['login_check_number']);
					
				if ($seccode != $code )
				{
					$this->result['err_msg']   =  $this->sys_conf_var['survey']['user']['login_error'];

					$this->tpl->assign("result",$this->result);

					$this->tpl->display("login.html");

					exit;
				}
				else
				{
					$this->__login($name,$pwd);
				}
			}
			else
			{
				//$_SESSION['login_error_num']+=1;
				$this->__login($name,$pwd);
			}

			$this->tpl->assign("result",$this->result);
			
			$this->tpl->display($tpl_name);
		}
		else
		{
			$this->tpl->display($tpl_name);
			exit;
		}

	}

	/**
	 * 登录子方法
	 * @return response
	 */
	function __login($name,$pwd)
	{	
		$userinfo = $this->user_model->login($name,$pwd);
		
		if(!empty($userinfo['u_id'])&&$userinfo['u_status'] != 0)
		{
			$_SESSION['login_error_num'] = 0;
			$_SESSION['SURVEY']['USER']  = $userinfo;
			//$_SESSION['SURVEY']['USER']['PERMISSION'] = $this->dealers_model->getPermission($uid);

			goto(get_url('admin/admin_common'),'header','document',false);
			exit;
		}
		else
		{
			$this->result['err_msg'] = $this->sys_conf_var['survey']['user']['login_error'];

			$this->tpl->assign("result",$this->result);

			$this->tpl->display("login.html");

			exit;
		}
	}

	/**
	 * 用户注销
	 * @return response
	 */
	function logout()
	{
		$tpl_name = "login.html";

		unset($_SESSION['SURVEY']['USER']);

		$this->tpl->display($tpl_name);
	}

	/**
	 * 禁止通行
	 * @return response
	 */
	function forbid()
	{
		$tpl_name = "forbid.html";

		$this->tpl->display($tpl_name);
	}

	/**
	 * 忘记密码
	 * @return response
	 */
	function forget()
	{
		$method = $this->in_var['request_method'];

		if($method == "post")
		{
			$tpl_name = "forget.html";
			$uid      = trim(intval($this->int_var['uid']));
			$email    = trim($this->int_var['email']);

			if($this->userbase_model->getLostPwd($uid,$email))
			{
				$this->result['msg'] = "发送成功";
			}
			else
			{
				$this->result['msg'] = "发送失败";
			}
		}

		$this->tpl->assign("result",$this->result);

		$this->tpl->display($tpl_name);
	}

	/**
	 * 是否登录
	 * @return response
	 */
	function isLogin()
	{
		return isset($_SESSION['SURVEY']['USER']);
	}

	/**
	 * 初始化管理数据
	 * @return response
	 */
	function initManageData()
	{
		$this->tpl->assign("manage",$_SESSION['SURVEY']['USER']);
	}
}